"""
author: yzh
date: 2021/1/21
"""
import requests
import re
import execjs


url = "https://webvpn.ecut.edu.cn/users/sign_in"
vpn_key_url = 'https://webvpn.ecut.edu.cn/vpn_key/update'
r_jwxt_url = 'https://jwxt-18801.webvpn.ecut.edu.cn/caslogin.jsp'
r_jwxt_login_url = 'https://authserver-443.webvpn.ecut.edu.cn/authserver/login?service=https%3A%2F%2Fjwxt-18801.webvpn.ecut.edu.cn%3A443%2Fcaslogin.jsp'
iPlanetDirectoryPro = ["",""]
getltList = ["","","","",""]
old_JSESSIONID = [""]
getkeyList = ["","",""]
webvpncookieList = [""]
tokenList = ["", ""]

JWXTIndexUrl = 'https://jwxt-18801.webvpn.ecut.edu.cn/caslogin.jsp'
MainFrameUrl = 'https://jwxt-18801.webvpn.ecut.edu.cn/menu/mainFrame.jsp'
MenuUrl = 'https://jwxt-18801.webvpn.ecut.edu.cn/menu/menu.jsp'
GradeLnAllActionUrl = 'https://jwxt-18801.webvpn.ecut.edu.cn/gradeLnAllAction.do?type=ln&oper=qb'
QueryGradeUrl = 'https://jwxt-18801.webvpn.ecut.edu.cn/gradeLnAllAction.do?type=ln&oper=qbinfo&lnxndm=2020-2021%D1%A7%C4%EA%B5%DA%D2%BB%D1%A7%C6%DA(%C1%BD%D1%A7%C6%DA)'

def querygrade(username, password):

    headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3775.400 QQBrowser/10.6.4208.400"
        }
    req = requests.get("https://webvpn.ecut.edu.cn/users/sign_in", headers=headers)
    tokenList[0] = re.findall(r'name="csrf-token" content="(.*?)"', req.text)[0]
    tokenList[1] = req.headers['set-cookie']


    r_vpn_login_data = {
        'utf8': '%E2%9C%93',  # ✓
        'authenticity_token': tokenList[0],
        'user[login]': username,
        'user[password]': password,
        'user[dymatice_code]': 'unknown',
        "user[otp_with_capcha]": "false",
        'commit': '登录 Login'
    }
    r_vpn_login_headers = {
        "Host": "webvpn.ecut.edu.cn",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3775.400 QQBrowser/10.6.4208.400",
        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
        "Accept-Language": "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2",
        "Accept-Encoding": "gzip, deflate, br",
        "Cache-Control": "max - age = 0",
        "Connection": "keep-alive",
        "Referer": "https://webvpn.ecut.edu.cn/users/sign_in",
        "Cookie": tokenList[1],
        "Upgrade-Insecure-Requests": "1"
    }
    r_vpn = requests.post('https://webvpn.ecut.edu.cn/users/sign_in', data = r_vpn_login_data, headers = r_vpn_login_headers, allow_redirects = False)
    webvpncookieList[0] = r_vpn.headers['set-cookie']

    r_vpn_key_headers = {
        'User-Agent': 'Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 4 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19',
        "Pragma": "no-cache",
        "Accept": "application/json, text/plain, */*",
        "Cookie": webvpncookieList[0],
        "Upgrade-Insecure-Requests": "1"

    }

    r_vpn_key = requests.get(vpn_key_url, headers=r_vpn_key_headers, allow_redirects=False)

    getkeyList[0] = 'webvpn_username=' + re.findall(r'webvpn_username=(.*?);', r_vpn_key.headers['set-cookie'])[0] + '; '
    getkeyList[1] = '_webvpn_key=' + re.findall(r'_webvpn_key=(.*?);', r_vpn_key.headers['set-cookie'])[0] + '; '
    getkeyList[2] = r_vpn_key.headers['set-cookie']


    r_jwxt_login_headers = {
        'User-Agent': 'Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 4 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19',
        "Pragma": "no-cache",
        "Accept": "application/json, text/plain, */*",
        "Cookie": getkeyList[1] + getkeyList[0]
    }
    r_JSESSIONID = requests.get(r_jwxt_url, headers=r_jwxt_login_headers, allow_redirects=False)
    old_JSESSIONID[0] = 'JSESSIONID='+re.findall(r'JSESSIONID=(.*?);', r_JSESSIONID.headers['set-cookie'])[0]+'; '

    r_jwxt_login_headers = {
        'User-Agent': 'Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 4 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19',
        "Pragma": "no-cache",
        "Accept": "application/json, text/plain, */*",
        "Cookie": getkeyList[2]
    }
    r_jwxt = requests.get(r_jwxt_login_url, headers=r_jwxt_login_headers)

    getltList[0] = re.findall(r'name="lt" value="(.*?)"', r_jwxt.text)[0]
    getltList[1] = re.findall(r'name="execution" value="(.*?)"', r_jwxt.text)[0]
    getltList[2] = re.findall(r'pwdDefaultEncryptSalt = "(.*?)"', r_jwxt.text)[0]
    getltList[3] = 'route=' + re.findall(r'route=(.*?),', r_jwxt.headers['set-cookie'])[0] + ';'
    getltList[4] = 'JSESSIONID=' + re.findall(r'JSESSIONID=(.*?);', r_jwxt.headers['set-cookie'])[0] + ';'

    with open('apps/DjangoWeb/spider/tools/encrypt.wisedu.js', encoding='utf-8') as f:
        code = f.read()
        js_compile = execjs.compile(code)
        pwd1 = js_compile.call('encryptAES', password, getltList[2])

    r_jwxt_login_data = {
        'username': username,
        'password': pwd1,  # 需加密
        'lt': getltList[0],
        'dllt': 'userNamePasswordLogin',
        'execution': getltList[1],
        '_eventId': 'submit',
        'rmShown': 1
    }

    r_jwxt_login_headers = {
        'User-Agent': 'Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 4 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19',
        "Pragma": "no-cache",
        "Accept": "application/json, text/plain, */*",
        "Cookie": getltList[3] + getkeyList[1] + getkeyList[0] + getltList[4]
    }
    r_jwxt_login = requests.post(r_jwxt_login_url, headers=r_jwxt_login_headers, data=r_jwxt_login_data, allow_redirects=False)

    # 第五次post登录教务系统
    iPlanetDirectoryPro[0] = 'iPlanetDirectoryPro='+re.findall(r'iPlanetDirectoryPro=(.*?);', r_jwxt_login.headers['set-cookie'])[0]+'; '
    iPlanetDirectoryPro[1] = re.findall(r'<a href="(.*?)"', r_jwxt_login.text)[0]

    UserAgent = "Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 4 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19"
    Cookie = getkeyList[1] + getkeyList[0] + old_JSESSIONID[0] + iPlanetDirectoryPro[0]

    # 进入ticket
    url = iPlanetDirectoryPro[1]
    TicketHeaders = {"User-Agent": UserAgent,"Referer": "https://authserver-443.webvpn.ecut.edu.cn/","Cookie": Cookie}
    requests.get(url, headers=TicketHeaders, allow_redirects=False)

    # 进入到主页
    JWXTIndexHeaders = {"User-Agent": UserAgent,"Referer": "https://authserver-443.webvpn.ecut.edu.cn/","Cookie": Cookie}
    requests.get(url=JWXTIndexUrl, headers=JWXTIndexHeaders)

    # 进入到caslogin
    MainFrameHeaders = {"User-Agent": UserAgent,"Referer": "https://jwxt-18801.webvpn.ecut.edu.cn/caslogin.jsp","Cookie": Cookie}
    requests.get(url=MainFrameUrl, headers=MainFrameHeaders)

    # 进入到mainFrame
    MenuHeaders = {"User-Agent": UserAgent,"Referer": "https://jwxt-18801.webvpn.ecut.edu.cn/menu/mainFrame.jsp","Cookie": Cookie}
    requests.get(url=MenuUrl, headers=MenuHeaders)

    # 进入到menu
    GradeLnAllActionHeaders = {"User-Agent": UserAgent,"Referer": "https://jwxt-18801.webvpn.ecut.edu.cn/menu/menu.jsp","Cookie": Cookie}
    requests.get(url=GradeLnAllActionUrl, headers=GradeLnAllActionHeaders)

    # 查询成绩
    QueryGradeHeaders = {"User-Agent": UserAgent,"Referer": "https://jwxt-ecut-edu-cn-18801.webvpn.ecit.cn/gradeLnAllAction.do?type=ln&oper=qb","Cookie": Cookie}
    response = requests.get(url=QueryGradeUrl, headers=QueryGradeHeaders)
    txt = response.text.replace("space","")
    a = ''  # 空字符(中间不加空格)

    for line in txt:
        a += line.strip()  # strip()是去掉每行末尾的换行符\n 1

        c = a.split()  # 将a分割成每个字符串 2

        b = ''.join(c)  # 将c的每个字符不以任何符号直接连接 3
    return a
